package cn.edu.sgu.www.mhxysy.controller.chongwu;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuSkillDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuSkill;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuSkillCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuSkillPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuSkillCategoryService;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuSkillService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.chongwu.ChongwuSkillVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宠物技能管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chongwu_skill", produces= MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChongwuSkillController {

	private final ChongwuSkillService chongwuSkillService;
	private final ChongwuSkillCategoryService chongwuSkillCategoryService;

	@Autowired
	public ChongwuSkillController(
			ChongwuSkillService chongwuSkillService,
			ChongwuSkillCategoryService chongwuSkillCategoryService) {
		this.chongwuSkillService = chongwuSkillService;
		this.chongwuSkillCategoryService = chongwuSkillCategoryService;
	}

	@ApiOperation("宠物认证前的验证")
	@RequestMapping(value = "/validate", method = RequestMethod.POST)
	public JsonResult<Void> validate(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		chongwuSkillService.validate(chongwuId);

		return JsonResult.success();
	}

	@ApiOperation("法术认证")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "location", value = "技能位置", required = true),
			@ApiImplicitParam(name = "chongwuId", value = "宠物ID", required = true)
	})
	@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
	public JsonResult<Void> authenticate(
			@NotNull(message = "技能位置" + CustomMessages.NOT_NULL) Integer location,
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		chongwuSkillService.authenticate(location, chongwuId);

		return JsonResult.success("认证成功");
	}

	@ApiOperation("认证取消")
	@RequestMapping(value = "/cancelAuth", method = RequestMethod.POST)
	public JsonResult<Void> cancelAuth(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		String name = chongwuSkillService.cancelAuth(chongwuId);

		return JsonResult.success("恭喜你已解除了" + name + "的法术认证，它的物理攻击力也将恢复正常");
	}

	@ApiOperation("学习技能")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "chongwuId", value = "宠物ID", required = true),
			@ApiImplicitParam(name = "skillId", value = "宠物技能类型ID", required = true)
	})
	@RequestMapping(value = "/study", method = RequestMethod.POST)
	public JsonResult<?> study(
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId,
			@NotNull(message = "宠物技能类型ID" + CustomMessages.NOT_NULL) Integer skillId) {
		String name = chongwuSkillService.study(chongwuId, skillId);

		if (StringUtils.isNullOrEmpty(name)) {
			return JsonResult.success("study");
		}

		return JsonResult.success("replace", name);
	}

	@ApiOperation("认证替换")
	@RequestMapping(value = "/replaceAuth", method = RequestMethod.POST)
	public JsonResult<Void> replaceAuth(
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId,
			@NotNull(message = "宠物技能类型ID" + CustomMessages.NOT_NULL) Integer skillId) {
		chongwuSkillService.replaceAuth(chongwuId, skillId);

		return JsonResult.success("替换成功");
	}

	@ApiOperation("添加宠物技能")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ChongwuSkillDTO chongwuSkillDTO) {
		chongwuSkillService.insert(chongwuSkillDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID修改宠物技能")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ChongwuSkillDTO chongwuSkillDTO) {
		chongwuSkillService.updateById(chongwuSkillDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("判断宠物是否有认证的技能")
	@RequestMapping(value = "/hasAuthedSkills", method = RequestMethod.GET)
	public JsonResult<Boolean> hasAuthedSkills(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		ChongwuSkill chongwuSkill = chongwuSkillService.getAuthedSkill(chongwuId);

		return JsonResult.success(null, chongwuSkill != null);
	}

	@ApiOperation("获取宠物认证提示信息")
	@RequestMapping(value = "/getAuthenticationMessage", method = RequestMethod.GET)
	public JsonResult<Void> getAuthenticationMessage(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		ChongwuSkill chongwuSkill = chongwuSkillService.getAuthedSkill(chongwuId);

		if (chongwuSkill == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "该宠物没有已认证的技能！");
		} else {
			ChongwuSkillCategory chongwuSkillCategory = chongwuSkillCategoryService.selectById(chongwuSkill.getSkillId());

			String message = "你确定要取消认证技能吗？确定取消将会失去<span class='green'>" + chongwuSkillCategory.getName() + "</span>技能。取消认证不花费银币。";

			return JsonResult.success(message);
		}
	}

	@ApiOperation("查询宠物已学习的技能")
	@RequestMapping(value = "/getLearnedSkills", method = RequestMethod.GET)
	public JsonResult<List<ChongwuSkillVO>> getLearnedSkills(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		List<ChongwuSkillVO> list = chongwuSkillService.getLearnedSkills(chongwuId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询宠物可认证的技能")
	@RequestMapping(value = "/getCanAuthedSkills", method = RequestMethod.GET)
	public JsonResult<List<ChongwuSkill>> getCanAuthedSkills(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = false)
			@RequestParam(required = false) String chongwuId) {
		List<ChongwuSkill> list = chongwuSkillService.getCanAuthedSkills(chongwuId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页查询宠物技能列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<ChongwuSkill>> selectByPage(ChongwuSkillPager pager) {
		Page<ChongwuSkill> page = chongwuSkillService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}